package com.enn.demo.web.controller.controller.sys;

import com.enn.demo.biz.service.impl.application.SysPersonApplicationService;
import com.enn.demo.biz.service.impl.application.SysPersonQueryService;
import com.enn.demo.biz.service.impl.application.command.PersonCommand;
import com.enn.demo.common.CommonConstant;
import com.enn.demo.core.service.impl.util.Page;
import com.enn.demo.core.service.impl.util.validator.ValidatorUtils;
import com.enn.demo.core.service.impl.util.validator.group.AddGroup;
import com.enn.demo.web.controller.common.AbstractController;
import com.enn.demo.web.controller.common.Result;
import com.enn.demo.web.controller.util.log.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author kll
 * @date 2021/5/14
 */
@Api(tags = "人员管理")
@RestController
@RequestMapping("/sys/person")
public class PersonController extends AbstractController {

    @Autowired
    private SysPersonQueryService sysPersonQueryService;

    @Autowired
    private SysPersonApplicationService sysPersonApplicationService;

    /**
     * 用户分页查询
     */
    @ApiOperation("用户分页查询")
    @GetMapping("/list")
    @RequiresPermissions("sys:person:list")
    public Result list(@RequestParam Map<String, Object> params){
        Page page = sysPersonQueryService.queryPage(params);
        return Result.ok().put(CommonConstant.PAGE, page);
    }

    /**
     * 保存用户
     */
    @ApiOperation("保存用户")
    @SysLog("保存用户")
    @PostMapping("/save")
    @RequiresPermissions("sys:person:save")
    public Result save(@RequestBody PersonCommand personCommand){
        ValidatorUtils.validateEntity(personCommand, AddGroup.class);
        sysPersonApplicationService.savePerson(personCommand);
        return Result.ok();
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @SysLog("删除用户")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:person:delete")
    public Result delete(@PathVariable("id") String id){
        sysPersonApplicationService.deletePerson(id);
        return Result.ok();
    }

    /**
     * 禁用用户
     */
    @ApiOperation("禁用用户")
    @SysLog("禁用用户")
    @PostMapping("/disable/{id}")
    @RequiresPermissions("sys:person:disable")
    public Result disable(@PathVariable("id") String id){
        sysPersonApplicationService.disablePerson(id);
        return Result.ok();
    }
}
